import { Component, OnInit } from '@angular/core';
import { UploadFile, MessageService } from '@iux/live';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-demo3',
  templateUrl: './demo3.component.html',
  styles: []
})
export class Demo3Component implements OnInit {

  filters = [];
  constructor(private messageService: MessageService) { }

  ngOnInit() {
    this.filters = [{
      name: 'suffix',
      filterFn: (files: UploadFile[]) => {
        const supportSuffix = ['png', 'jpg'];
        let validFiles = files.filter(file => {
          let suffix = file.name.split('.').pop();
          return supportSuffix.includes(suffix);
        });

        if (validFiles.length !== files.length) {
          this.messageService.error(`包含文件格式不正确，只支持.png,.jpg格式`);
        }
        return validFiles;
      }
    }, {
      name: 'async',
      filterFn: (files: UploadFile[]) => {
        return new Observable((observer) => {

          let validFiles = files.filter(file => {
            let filename = file.name.split('.')[0];
            return filename.includes('a');
          });

          if (validFiles.length !== files.length) {
            this.messageService.error(`文件名必须包含字母a`);
          }

          observer.next(validFiles);
          observer.complete();
          
        });
      }
    }];
  }

  change(e) {
    console.log(e);
  }

}
